class OutletStatusesController < ApplicationController
  # GET /outlet_statuses
  # GET /outlet_statuses.json
  def index
    @outlet_statuses = OutletStatus.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @outlet_statuses }
    end
  end

  # GET /outlet_statuses/1
  # GET /outlet_statuses/1.json
  def show
    @outlet_status = OutletStatus.find(params[:id])

    respond_to do |format|
      format.html { redirect_to dashboard_path } # show.html.erb
      format.json { render json: @outlet_status }
    end
  end

  # GET /outlet_statuses/new
  # GET /outlet_statuses/new.json
  def new
    @outlet_status = OutletStatus.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @outlet_status }
    end
  end

  # GET /outlet_statuses/1/edit
  def edit
    @outlet_status = OutletStatus.find(params[:id])
  end

  # POST /outlet_statuses
  # POST /outlet_statuses.json
  def create
    @outlet_status = OutletStatus.new(params[:outlet_status])

    respond_to do |format|
      if @outlet_status.save
        format.html { redirect_to @outlet_status, notice: 'Outlet status was successfully created.' }
        format.json { render json: @outlet_status, status: :created, location: @outlet_status }
      else
        format.html { render action: "new" }
        format.json { render json: @outlet_status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /outlet_statuses/1
  # PUT /outlet_statuses/1.json
  def update
    @outlet_status = OutletStatus.find(params[:id])
    
    respond_to do |format|
      if @outlet_status.update_attributes(params[:outlet_status])  	
        format.html { redirect_to dashboard_path, notice: 'Outlet status was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @outlet_status.errors, status: :unprocessable_entity }
      end
    end
  end

  def update_outlet
  	@outlet_status = OutletStatus.find(params[:id])
  	if @outlet_status.status == true
  	    #var = @outlet_status.or_time
  		@outlet_status.status = false
  		@outlet_status.user_override = true
  		@outlet_status.or_time = UtilityFunctions.current_time[2].strftime("%Y-%m-%d %H:%M:%S") # - (5*60*60)
  	else
  		@outlet_status.status = true
  		#var = @outlet_status.or_time
  		@outlet_status.user_override = true
  		@outlet_status.or_time = UtilityFunctions.current_time[2].strftime("%Y-%m-%d %H:%M:%S") # Time.now - (5*60*60)
  	end

  	respond_to do |format|
  	  if @outlet_status.update_attributes(params[:outlet_status])

  	    ilms = SerialInterface.new

  	    ilms.send_outlet([@outlet_status.outlet_id])
  	    #SerialInterface.send_outlet([@outlet_status.outlet_id]) 
  	    format.html {redirect_to dashboard_path }
  	    format.json {head :ok }
  	  else
  	  	format.html { render action: "edit" }
  	  	format.json { render json: @outlet_status.errors, status: :unprocessable_entity }
  	  end
    end
  end
  
  # DELETE /outlet_statuses/1
  # DELETE /outlet_statuses/1.json
  def destroy
    @outlet_status = OutletStatus.find(params[:id])
    @outlet_status.destroy

    respond_to do |format|
      format.html { redirect_to outlet_statuses_url }
      format.json { head :ok }
    end
  end
end
